﻿using System;
using System.Windows;
using System.Windows.Input;
using DependencyAnalysis.DataModel;
using theeg.DependencyAnalysis.CommonAddin.Models;
using theeg.GraphWpf.Models;

namespace theeg.DependencyAnalysis.CommonAddin.Views
{
    public partial class ProximityGraph
    {
        public ICommand ChangeDescriptorCommand
        {
            get { return (ICommand)GetValue(ChangeDescriptorCommandProperty); }
            set { SetValue(ChangeDescriptorCommandProperty, value); }
        }

        public static readonly DependencyProperty ChangeDescriptorCommandProperty =
            DependencyProperty.Register("ChangeDescriptorCommand", typeof(ICommand), typeof(ProximityGraph), new PropertyMetadata());

        public GraphParameter GraphContent
        {
            get { return (GraphParameter)GetValue(GraphContentProperty); }
            set { SetValue(GraphContentProperty, value); }
        }

        public static readonly DependencyProperty GraphContentProperty =
            DependencyProperty.Register("GraphContent", typeof(GraphParameter), typeof(ProximityGraph), new PropertyMetadata());

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            if (e.Property == GraphContentProperty)
            {
                Sketch.Nodes.Clear();
                Sketch.Transitions.Clear();
                if (GraphContent != null)
                {
                    foreach (var node in GraphContent.Nodes)
                    {
                        Sketch.Nodes.Add(node);
                    }
                    foreach (var transition in GraphContent.Transitions)
                    {
                        Sketch.Transitions.Add(transition);
                    }
                    Sketch.Arrangement = null;
                    Sketch.Arrange();
                    var descriptor = GraphContent.Descriptor;

                    history.CurrentItem = descriptor;
                    CommandManager.InvalidateRequerySuggested();
                }
            }
            base.OnPropertyChanged(e);
        }

        public ProximityGraph()
        {
            InitializeComponent();
        }

        public void ChangeDescriptor(IDescriptor descriptor)
        {
            var command = ChangeDescriptorCommand;
            if (command == null) return;
            var changeDescriptorParameter = new GraphParameter { Descriptor = descriptor };
            command.Execute(changeDescriptorParameter);
        }

        private void ClickGraphItem(object sender, RoutedEventArgs e)
        {
            var node = e.OriginalSource as Node;
            if (node != null && (node.DataObject is IDescriptor))
            {
                ChangeDescriptor(node.DataObject as IDescriptor);
                return;
            }
            var transition = e.OriginalSource as Transition;
            if (transition != null)
            {
                var transitionDescriptor = new TransitionDescriptor
                    {
                        Source = transition.Source.DataObject as IDescriptor,
                        Target = transition.Target.DataObject as IDescriptor
                    };
                ChangeDescriptor(transitionDescriptor);
            }
        }

        private void HistoryPropertyChanged(object sender, EventArgs e)
        {
            ChangeDescriptor(history.CurrentItem as IDescriptor);
        }
    }
}
